<template>
  <section class="manage-section">
    <section class="section_main">
<!--      <selector></selector>-->
      <section class="section_main_table">
        <el-table
          :data="tableData"
          height="65vh"
          v-loading="loading">
          <el-table-column v-if="isSelection" type="selection" width="55"></el-table-column>

          <el-table-column v-if="isIndex" type="index" label="ID" width="80" align="center"></el-table-column>

          <template v-for="item in tableColumn">
            <slot v-if="item.slot" :name="item.slot"></slot>

            <el-table-column
              v-else
              :key="item.prop"
              :prop="item.prop"
              :label="item.label"
              :width="item.width"
              show-overflow-tooltip
              align="center"
            ></el-table-column>
          </template>
        </el-table>
        <el-pagination
          style="margin-top: 30px;float: right"
          :page-size="100"
          @current-change="reloadPage"
          layout="total, prev, pager, next, jumper"
          :current-page.sync="currentPage"
          :total=total />
      </section>
    </section>
<!--    <edit-dialog-->
<!--      :visible-flag='dialogData.visibleFlag'-->
<!--      :data="dialogData.editData"-->
<!--      @close-dialog="closeDialog"/>-->
  </section>
</template>

<script>
import request from '@/network/request'
// import editDialog from './editDialog'
export default {
  components: {
    // editDialog
  },
  props: {
    tableColumn: { type: Array, required: true },
    isIndex: { type: Boolean, default: false },
    isSelection: { type: Boolean, default: false },
    apiPath: { type: String },
    openDialog: { type: Function },
    selectPath: { type: String }
  },
  beforeMount () {
    this.getData()
  },
  created () {
    this.$bus.$off('timeChange')
    this.$bus.$on('timeChange', (data) => {
      console.log('有组件调用了bus的on！然后调用了timeChange!', data)
      this.dateDuring = data
      this.getData()
    })
    this.$bus.$off('refresh')
    this.$bus.$on('refresh', (data) => {
      console.log('有组件调用了bus的on！然后调用了refresh!', data)
      this.dateDuring = data
      this.getData()
    })
  },
  beforeDestroy () {
    // this.$bus.$off('timeChange')
  },
  data () {
    return {
      dialogData: {
        visibleFlag: false,
        dialogTitle: '新增',
        formType: 'add',
        editData: {}
      },
      tableData: [],
      selectionArr: [],
      loading: false,
      pageSize: 100,
      currentPage: 1,
      total: 0,
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      dateDuring: ''
    }
  },
  provide () {
    return {
      dateDuring: this.dateDuring,
      reloadPage: this.reloadPage,
      getData: this.getData,
      getDateDuring: this.getDateDuring,
      refresh: this.refresh
    }
  },
  methods: {
    getData () {
      this.loading = true
      const params = {
        pageSize: this.pageSize,
        currentPage: this.currentPage,
        startTime: null,
        endTime: null,
        factory: this.$store.getters.getFactory
      }
      if (this.dateDuring !== null && this.dateDuring !== '' && this.dateDuring.length === 2) {
        params.startTime = (this.dateDuring[0].getTime()) - 1000 * 60 * 60 * 24
        params.endTime = (this.dateDuring[1].getTime()) + 1000 * 60 * 60 * 24
      }
      // 默认值
      let tempUrl = this.apiPath + '/select'
      if (this.selectPath !== undefined) {
        tempUrl = this.apiPath + this.selectPath
      }
      // 堆肥月保养接口规则不同，需要判断
      if (this.apiPath === '/shlj/sjqCompostProduceMaintenance/select_month') {
        tempUrl = this.apiPath
      }
      if (this.apiPath === '/shlj/sjqCompostProduceMaintenance/select_day') {
        tempUrl = this.apiPath
      }
      request({
        method: 'post',
        url: tempUrl,
        params: params
      }).then((res) => {
        this.tableData = res.data.data.records
        console.log('表格数据是：：：：：：：：：：：：：', this.tableData)
        this.total = res.data.data.sum
        this.loading = false
      })
    },
    // openDialog (formType, editData = {}) {
    //   this.dialogData = {
    //     visibleFlag: true,
    //     formType,
    //     editData,
    //     dialogTitle: formType === 'add' ? '新增' : '编辑'
    //   }
    // },
    refresh () {
      this.dateDuring = null
      this.currentPage = 1
      this.getData()
    },
    handleDelete (index, row) {
      this.$confirm('此操作将删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest(row)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    getDateDuring (obj) {
      this.dateDuring = obj
      console.log('editTable父组件接受的时间范围为：', this.dateDuring)
    },
    handleArrDelete () {
      if (this.selectionArr.length === 0) {
        this.$alert('请选择要删除的记录', '提醒', {
          confirmButtonText: '确定'
        })
      } else {
        this.$confirm('此操作将删除该记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          for (let i = 0; i < this.selectionArr.length; i++) {
            this.deleteRequest(this.selectionArr[i])
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    },
    deleteRequest (record) {
      console.log(record)
      // 默认值
      let tempUrl = this.apiPath + '/delete'
      // 堆肥月保养接口规则不同，需要判断
      if (this.apiPath === '/shlj/sjqCompostProduceMaintenance/select_month') {
        tempUrl = '/shlj/sjqCompostProduceMaintenance/delete_month'
      }
      if (this.apiPath === '/shlj/sjqCompostProduceMaintenance/select_day') {
        tempUrl = '/shlj/sjqCompostProduceMaintenance/delete_day'
      }
      request({
        method: 'post',
        // url: this.apiPath + '/delete',
        url: tempUrl,
        params: {
          id: record.id,
          factory: this.$store.getters.getFactory
        }
      }).then((res) => {
        if (res.data.code === 200) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getData()
        } else {
          this.$message.error('删除失败!')
        }
      })
    },
    reloadPage (currentPage) {
      this.currentPage = currentPage
      this.getData()
    },
    closeDialog () {
      this.dialogData.visibleFlag = false
      this.getData()
    },
    timeChange () {
      // console.log(this.value2[0].getTime())
      console.log('获取时间')
      console.log(this.dateDuring[1].getTime())
      this.currentPage = 1
      this.getData()
    }
  }
}
</script>

<style scoped>
</style>
